package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.security.provider.MD4;

import java.util.List;

/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

	@Reference
	private CheckGroupService checkGroupService;

	/**
	 * 新增检查组
	 * @param checkGroup
	 * @param checkitemIds
	 * @return
	 */
	@RequestMapping("/add")
	public Result addCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){

		try {
			checkGroupService.add(checkGroup,checkitemIds);
			return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);

	}

	/**
	 * 分页查询检查组
	 * @param queryPageBean
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

			PageResult pageResult = checkGroupService.findPage(queryPageBean);
			return pageResult;

	}

	/**
	 *通过id查询检查组
	 * @param id
	 * @return
	 */
	@RequestMapping("/findgroupById")
	public Result findGroupById(Integer id){
		CheckGroup checkGroup = checkGroupService.findgroupById(id);
		//mybatis执行查询操作，没有查询出来会返回null，而jdbcTemplate没有查询出来会直接报错
		if (checkGroup != null) {
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
		}
		return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
	}

	/**
	 * 编辑检查组
	 * @param checkGroup
	 * @param checkitemIds
	 * @return
	 */
	@RequestMapping("/edit")
	public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){

		try {
			checkGroupService.edit(checkGroup,checkitemIds);
			return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
	}

	/**
	 * 删除检查组
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Integer id){

		try {
			checkGroupService.delete(id);
			return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true, MessageConstant.DELETE_CHECKGROUP_FAIL);
		}

	}

	@RequestMapping("/findAll")
	public Result findAll(){

		try {
			List<CheckGroup> checkGroupList = checkGroupService.findAll();
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}

	}

}
